﻿@namespace CyssBlazor.Shared.Components


<label class="form-label" for="@Id">部门</label>
<div class="form-group-input" style="margin-left:-5px">
    <div class="input-group">
        <input type="text" id="@Id" class="form-control valid" @bind-value="CurrentValueString" readonly="readonly">
        <div class="input-group-append">
            <button type="button" @onclick="OnSelect" class="btn btn-primary btn btn-browser btn-primary" role="button">
                <i class="fa fa-folder-open-o"></i><span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">选择</font></font></span>
            </button>
        </div>
    </div>
</div>
<Modal @ref="Modal" Size=szf>
    <ModalDialog Title="部门列表" IsCentered="true" Size=Size.Small>
        <BodyTemplate>
            <Tree Items="@TreeItems" OnTreeItemClick="OnTreeItemChecked" ShowCheckbox="false" />
        </BodyTemplate>
    </ModalDialog>
</Modal>





@code {

    private Modal Modal;

    private string Id = Guid.NewGuid().ToString().Replace("-", "");


    private List<TreeItem> TreeItems { set; get; } = new List<TreeItem>();

    private async Task OnTreeItemChecked(TreeItem item)
    {
        CurrentValueString = (item.Tag as DepartmentModel).Name;
        Value = (int)item.Key;
        await Modal.Toggle();
        await InvokeAsync(StateHasChanged);
    }


    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await GetOrganizationSelectedItems();
        }
        await base.OnAfterRenderAsync(firstRender);
    }


    private async Task OnSelect()
    {
        await Modal.Toggle();
    }

    private int _value;

    [Parameter]
    public int Value
    {
        get => _value;
        set
        {
            if (_value == value) return;
            _value = value;
            ValueChanged.InvokeAsync(value);
        }
    }
    [Parameter]
    public string DisplayText { get; set; }

    [Parameter]
    public EventCallback<int> ValueChanged { get; set; }

    public string CurrentValueString { set; get; }

    private async Task GetOrganizationSelectedItems()
    {
        var apiResponse = await AccountClientFactory.Dept.GetDeptTrees();
        if (apiResponse.IsSuccess)
        {
            SetTreeItems(apiResponse.Data);
        }
        await InvokeAsync(StateHasChanged);
    }

    private void SetTreeItems(IEnumerable<DeptTreeModel> deptTreeModels)
    {
        foreach (var deptTree in deptTreeModels)
        {
            if (deptTree.Dept.Id == Value)
            {
                CurrentValueString = deptTree.Dept.Name;
            }
            TreeItem treeItem = new TreeItem();
            treeItem.Key = deptTree.Dept.Id;
            treeItem.Text = deptTree.Dept.Name;
            treeItem.Tag = deptTree.Dept;
            TreeItems.Add(treeItem);
            RecursionTree(treeItem, deptTree);
        }
    }
    /// <summary>
    /// 递归构建权限树
    /// </summary>
    /// <param name="menuTree"></param>
    /// <param name="menus"></param>
    private void RecursionTree(TreeItem treeItem, DeptTreeModel deptTreeModel)
    {
        if (deptTreeModel.Childrens != null && deptTreeModel.Childrens.Count > 0)
        {
            List<TreeItem> treeItems = new List<TreeItem>();
            treeItem.Items = treeItems;
            foreach (var v_deptTree in deptTreeModel.Childrens)
            {
                if (v_deptTree.Dept.Id == Value)
                {
                    CurrentValueString = v_deptTree.Dept.Name;
                }
                TreeItem temp_treeItem = new TreeItem();
                temp_treeItem.Key = v_deptTree.Dept.Id;
                temp_treeItem.Text = v_deptTree.Dept.Name;
                temp_treeItem.Tag = v_deptTree.Dept;
                treeItems.Add(temp_treeItem);
                RecursionTree(temp_treeItem, v_deptTree);
            }
        }
    }
}